package com.zhkj.shixi.tvfilemanager;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.WindowManager;


import java.io.File;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.*;

public class StaticDeviceInsertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(staticReceiverAvailability) {
            if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
                Log.w("USB", "USB Insert");
               showNormalDialog(context);

            } else  if(intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
                String USBPath=intent.getData().getPath();
                for (File device : deviceList)
                {
                    if(device.getAbsolutePath().equals(USBPath))
                    {
                        deviceList.remove(device);
                        Log.w("USB","USB Out"+"\n"+device.getAbsolutePath());
                        break;
                    }
                }

            }
        }
    }


    private void showNormalDialog(final Context context){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setTitle("USB Device Insert");
        normalDialog.setMessage("是否启动文件管理器？");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.w("dialog","press ok");
                        FileManagerAcitivity fileManagerAcitivity = new FileManagerAcitivity();
                        fileManagerAcitivity.startAction(context);
                        //TODO 启动文件管理器
                        dialog.dismiss();
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.w("dialog","press cancel");
                        dialog.dismiss();
                    }
                });
        // 显示
        AlertDialog dialog = normalDialog.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
        dialog.show();
    }
}
